
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Reactor模式 · 高性能服务器开发 技术专栏</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="easy_coder">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-prism/prism.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-theme-fexa/fexa.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="实例：一个服务器程序的架构介绍.html" />
    
    
    <link rel="prev" href="主线程与工作线程的分工.html" />
    

    </head>
    <body>
        
<div class="book">
	<div class="header-inner">
		<!-- LOGO -->
		<div class="logo"></div>
		<span class="title"></span>

		<!-- Search -->
		
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>


		<!-- Nav -->
		<ul class="header-nav">
			
		</ul>
	</div>

	<div class="book-summary">
		<div class="book-summary-title">文档目录</div>
		
		
		<nav role="navigation">
			


<ul class="summary">
    
    

    

    
        
        <li class="header">Part I</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../C++必知必会的知识点/">
            
                <a href="../C++必知必会的知识点/">
            
                    
                    C++必知必会的知识点
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../C++必知必会的知识点/如何成为一名合格的CC++开发者？.html">
            
                <a href="../C++必知必会的知识点/如何成为一名合格的CC++开发者？.html">
            
                    
                    如何成为一名合格的C/C++开发者？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../C++必知必会的知识点/不定参数函数实现var_arg系列的宏.html">
            
                <a href="../C++必知必会的知识点/不定参数函数实现var_arg系列的宏.html">
            
                    
                    不定参数函数实现var_arg系列的宏
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../C++必知必会的知识点/你一定要搞明白的C函数调用方式与栈原理.html">
            
                <a href="../C++必知必会的知识点/你一定要搞明白的C函数调用方式与栈原理.html">
            
                    
                    你一定要搞明白的C函数调用方式与栈原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../C++必知必会的知识点/深入理解CC++中的指针.html">
            
                <a href="../C++必知必会的知识点/深入理解CC++中的指针.html">
            
                    
                    深入理解C/C++中的指针
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../C++必知必会的知识点/详解C++11中的智能指针.html">
            
                <a href="../C++必知必会的知识点/详解C++11中的智能指针.html">
            
                    
                    详解C++11中的智能指针
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../C++必知必会的知识点/C++17结构化绑定.html">
            
                <a href="../C++必知必会的知识点/C++17结构化绑定.html">
            
                    
                    C++17结构化绑定
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../C++必知必会的知识点/C++必须掌握的pimpl惯用法.html">
            
                <a href="../C++必知必会的知识点/C++必须掌握的pimpl惯用法.html">
            
                    
                    C++必须掌握的pimpl惯用法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../C++必知必会的知识点/用VisualStudio调试Linux程序.html">
            
                <a href="../C++必知必会的知识点/用VisualStudio调试Linux程序.html">
            
                    
                    用Visual Studio调试Linux程序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../C++必知必会的知识点/如何使用VisualStudio管理和阅读开源项目代码.html">
            
                <a href="../C++必知必会的知识点/如何使用VisualStudio管理和阅读开源项目代码.html">
            
                    
                    如何使用Visual Studio管理和阅读开源项目代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="../C++必知必会的知识点/利用cmake工具生成VisualStudio工程文件.html">
            
                <a href="../C++必知必会的知识点/利用cmake工具生成VisualStudio工程文件.html">
            
                    
                    利用cmake工具生成Visual Studio工程文件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../多线程/">
            
                <a href="../多线程/">
            
                    
                    多线程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../多线程/后台C++开发你一定要知道的条件变量.html">
            
                <a href="../多线程/后台C++开发你一定要知道的条件变量.html">
            
                    
                    后台C++开发你一定要知道的条件变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../多线程/整型变量赋值是原子操作吗？.html">
            
                <a href="../多线程/整型变量赋值是原子操作吗？.html">
            
                    
                    整型变量赋值是原子操作吗？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../网络编程/">
            
                <a href="../网络编程/">
            
                    
                    网络编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../网络编程/bind函数重难点解析.html">
            
                <a href="../网络编程/bind函数重难点解析.html">
            
                    
                    bind 函数重难点解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../网络编程/connect函数在阻塞和非阻塞模式下的行为.html">
            
                <a href="../网络编程/connect函数在阻塞和非阻塞模式下的行为.html">
            
                    
                    connect 函数在阻塞和非阻塞模式下的行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../网络编程/select函数重难点解析.html">
            
                <a href="../网络编程/select函数重难点解析.html">
            
                    
                    select 函数重难点解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4" data-path="../网络编程/Linuxepoll模型（含LT模式和ET模式详解）.html">
            
                <a href="../网络编程/Linuxepoll模型（含LT模式和ET模式详解）.html">
            
                    
                    Linux epoll 模型（含LT 模式和 ET 模式详解）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.5" data-path="../网络编程/socket的阻塞模式和非阻塞模式.html">
            
                <a href="../网络编程/socket的阻塞模式和非阻塞模式.html">
            
                    
                    socket 的阻塞模式和非阻塞模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.6" data-path="../网络编程/非阻塞模式下send和recv函数的返回值.html">
            
                <a href="../网络编程/非阻塞模式下send和recv函数的返回值.html">
            
                    
                    非阻塞模式下 send 和 recv 函数的返回值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.7" data-path="../网络编程/服务器开发通信协议设计介绍.html">
            
                <a href="../网络编程/服务器开发通信协议设计介绍.html">
            
                    
                    服务器开发通信协议设计介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.8" data-path="../网络编程/TCP协议如何解决粘包、半包问题.html">
            
                <a href="../网络编程/TCP协议如何解决粘包、半包问题.html">
            
                    
                    TCP 协议如何解决粘包、半包问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.9" data-path="../网络编程/网络通信中收发数据的正确姿势.html">
            
                <a href="../网络编程/网络通信中收发数据的正确姿势.html">
            
                    
                    网络通信中收发数据的正确姿势
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.10" data-path="../网络编程/服务器端发数据时，如果对端一直不收，怎么办？.html">
            
                <a href="../网络编程/服务器端发数据时，如果对端一直不收，怎么办？.html">
            
                    
                    服务器端发数据时，如果对端一直不收，怎么办？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../程序员必知必会的网络命令/">
            
                <a href="../程序员必知必会的网络命令/">
            
                    
                    程序员必知必会的网络命令
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1.1" data-path="../程序员必知必会的网络命令/利用telnet命令发电子邮件.html">
            
                <a href="../程序员必知必会的网络命令/利用telnet命令发电子邮件.html">
            
                    
                    利用telnet命令发电子邮件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.2" data-path="../程序员必知必会的网络命令/做Java或者C++开发都应该知道的lsof命令.html">
            
                <a href="../程序员必知必会的网络命令/做Java或者C++开发都应该知道的lsof命令.html">
            
                    
                    做Java或者C++开发都应该知道的lsof命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.3" data-path="../程序员必知必会的网络命令/Linux网络故障排查的瑞士军刀.html">
            
                <a href="../程序员必知必会的网络命令/Linux网络故障排查的瑞士军刀.html">
            
                    
                    Linux网络故障排查的瑞士军刀nc命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.4" data-path="../程序员必知必会的网络命令/Linuxtcpdump使用介绍.html">
            
                <a href="../程序员必知必会的网络命令/Linuxtcpdump使用介绍.html">
            
                    
                    Linux tcpdump使用详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.5" data-path="../程序员必知必会的网络命令/从抓包的角度分析connect函数的连接过程.html">
            
                <a href="../程序员必知必会的网络命令/从抓包的角度分析connect函数的连接过程.html">
            
                    
                    从抓包的角度分析connect函数的连接过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.1.6" data-path="../程序员必知必会的网络命令/服务器开发中网络数据分析与故障排查经验漫谈.html">
            
                <a href="../程序员必知必会的网络命令/服务器开发中网络数据分析与故障排查经验漫谈.html">
            
                    
                    服务器开发中网络数据分析与故障排查经验漫谈
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">Part II</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="./">
            
                <a href="./">
            
                    
                    高性能服务器框架设计
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1.1" data-path="主线程与工作线程的分工.html">
            
                <a href="主线程与工作线程的分工.html">
            
                    
                    主线程与工作线程的分工
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="4.1.2" data-path="Reactor模式.html">
            
                <a href="Reactor模式.html">
            
                    
                    Reactor模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.3" data-path="实例：一个服务器程序的架构介绍.html">
            
                <a href="实例：一个服务器程序的架构介绍.html">
            
                    
                    实例：一个服务器程序的架构介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.4" data-path="错误码系统的设计.html">
            
                <a href="错误码系统的设计.html">
            
                    
                    错误码系统的设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.5" data-path="日志系统的设计.html">
            
                <a href="日志系统的设计.html">
            
                    
                    日志系统的设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.6" data-path="如何设计断线自动重连机制.html">
            
                <a href="如何设计断线自动重连机制.html">
            
                    
                    如何设计断线自动重连机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.7" data-path="心跳包机制设计详解.html">
            
                <a href="心跳包机制设计详解.html">
            
                    
                    心跳包机制设计详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.8" data-path="业务数据处理一定要单独开线程吗.html">
            
                <a href="业务数据处理一定要单独开线程吗.html">
            
                    
                    业务数据处理一定要单独开线程吗
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.1.9" data-path="C++高性能服务器网络框架设计细节.html">
            
                <a href="C++高性能服务器网络框架设计细节.html">
            
                    
                    C++ 高性能服务器网络框架设计细节
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../服务器开发案例实战/">
            
                <a href="../服务器开发案例实战/">
            
                    
                    服务器开发案例实战
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1.1" data-path="../服务器开发案例实战/从零实现一个http服务器.html">
            
                <a href="../服务器开发案例实战/从零实现一个http服务器.html">
            
                    
                    从零实现一个http服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.2" data-path="../服务器开发案例实战/从零实现一款12306刷票软件.html">
            
                <a href="../服务器开发案例实战/从零实现一款12306刷票软件.html">
            
                    
                    从零实现一款12306刷票软件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.3" data-path="../服务器开发案例实战/从零实现一个邮件收发客户端.html">
            
                <a href="../服务器开发案例实战/从零实现一个邮件收发客户端.html">
            
                    
                    从零实现一个邮件收发客户端
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.4" data-path="../服务器开发案例实战/从零开发一个WebSocket服务器.html">
            
                <a href="../服务器开发案例实战/从零开发一个WebSocket服务器.html">
            
                    
                    从零开发一个WebSocket服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.5" data-path="../服务器开发案例实战/1从一款多人联机实时对战游戏开始.html">
            
                <a href="../服务器开发案例实战/1从一款多人联机实时对战游戏开始.html">
            
                    
                    从零学习开源项目系列（一） 从一款多人联机实时对战游戏开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.6" data-path="../服务器开发案例实战/2最后一战概况.html">
            
                <a href="../服务器开发案例实战/2最后一战概况.html">
            
                    
                    从零学习开源项目系列（二） 最后一战概况
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.7" data-path="../服务器开发案例实战/3CSBattleMgr服务源码研究.html">
            
                <a href="../服务器开发案例实战/3CSBattleMgr服务源码研究.html">
            
                    
                    从零学习开源项目系列（三） CSBattleMgr服务源码研究
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.1.8" data-path="../服务器开发案例实战/4LogServer源码探究.html">
            
                <a href="../服务器开发案例实战/4LogServer源码探究.html">
            
                    
                    从零学习开源项目系列（四）LogServer源码探究
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">Part III</li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="../TeamTalk源码解析/">
            
                <a href="../TeamTalk源码解析/">
            
                    
                    TeamTalk IM源码分析
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1.1" data-path="../TeamTalk源码解析/01TeamTalk介绍.html">
            
                <a href="../TeamTalk源码解析/01TeamTalk介绍.html">
            
                    
                    01 TeamTalk介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.2" data-path="../TeamTalk源码解析/02服务器端的程序的编译与部署.html">
            
                <a href="../TeamTalk源码解析/02服务器端的程序的编译与部署.html">
            
                    
                    02 服务器端的程序的编译与部署
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.3" data-path="../TeamTalk源码解析/03服务器端的程序架构介绍.html">
            
                <a href="../TeamTalk源码解析/03服务器端的程序架构介绍.html">
            
                    
                    03 服务器端的程序架构介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.4" data-path="../TeamTalk源码解析/04服务器端db_proxy_server源码分析.html">
            
                <a href="../TeamTalk源码解析/04服务器端db_proxy_server源码分析.html">
            
                    
                    04 服务器端db_proxy_server源码分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.5" data-path="../TeamTalk源码解析/05服务器端msg_server源码分析.html">
            
                <a href="../TeamTalk源码解析/05服务器端msg_server源码分析.html">
            
                    
                    05 服务器端msg_server源码分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.6" data-path="../TeamTalk源码解析/06服务器端login_server源码分析.html">
            
                <a href="../TeamTalk源码解析/06服务器端login_server源码分析.html">
            
                    
                    06 服务器端login_server源码分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.7" data-path="../TeamTalk源码解析/07服务器端msfs源码分析.html">
            
                <a href="../TeamTalk源码解析/07服务器端msfs源码分析.html">
            
                    
                    07 服务器端msfs源码分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.8" data-path="../TeamTalk源码解析/08服务器端file_server源码分析.html">
            
                <a href="../TeamTalk源码解析/08服务器端file_server源码分析.html">
            
                    
                    08 服务器端file_server源码分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.9" data-path="../TeamTalk源码解析/09服务器端route_server源码分析.html">
            
                <a href="../TeamTalk源码解析/09服务器端route_server源码分析.html">
            
                    
                    09 服务器端route_server源码分析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.10" data-path="../TeamTalk源码解析/10开放一个TeamTalk测试服务器地址和几个测试账号.html">
            
                <a href="../TeamTalk源码解析/10开放一个TeamTalk测试服务器地址和几个测试账号.html">
            
                    
                    10 开放一个TeamTalk测试服务器地址和几个测试账号
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.1.11" data-path="../TeamTalk源码解析/11pc客户端源码分析.html">
            
                <a href="../TeamTalk源码解析/11pc客户端源码分析.html">
            
                    
                    11 pc客户端源码分析
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="7.1" data-path="../libevent源码深度剖析/">
            
                <a href="../libevent源码深度剖析/">
            
                    
                    libevent源码深度剖析
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1.1" data-path="../libevent源码深度剖析/libevent源码深度剖析01.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析01.html">
            
                    
                    libevent源码深度剖析01
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.2" data-path="../libevent源码深度剖析/libevent源码深度剖析02.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析02.html">
            
                    
                    libevent源码深度剖析02
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.3" data-path="../libevent源码深度剖析/libevent源码深度剖析03.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析03.html">
            
                    
                    libevent源码深度剖析03
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.4" data-path="../libevent源码深度剖析/libevent源码深度剖析04.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析04.html">
            
                    
                    libevent源码深度剖析04
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.5" data-path="../libevent源码深度剖析/libevent源码深度剖析05.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析05.html">
            
                    
                    libevent源码深度剖析05
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.6" data-path="../libevent源码深度剖析/libevent源码深度剖析06.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析06.html">
            
                    
                    libevent源码深度剖析06
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.7" data-path="../libevent源码深度剖析/libevent源码深度剖析07.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析07.html">
            
                    
                    libevent源码深度剖析07
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.8" data-path="../libevent源码深度剖析/libevent源码深度剖析08.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析08.html">
            
                    
                    libevent源码深度剖析08
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.9" data-path="../libevent源码深度剖析/libevent源码深度剖析09.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析09.html">
            
                    
                    libevent源码深度剖析09
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.10" data-path="../libevent源码深度剖析/libevent源码深度剖析10.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析10.html">
            
                    
                    libevent源码深度剖析10
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.11" data-path="../libevent源码深度剖析/libevent源码深度剖析11.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析11.html">
            
                    
                    libevent源码深度剖析11
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.12" data-path="../libevent源码深度剖析/libevent源码深度剖析12.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析12.html">
            
                    
                    libevent源码深度剖析12
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.1.13" data-path="../libevent源码深度剖析/libevent源码深度剖析13.html">
            
                <a href="../libevent源码深度剖析/libevent源码深度剖析13.html">
            
                    
                    libevent源码深度剖析13
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="../leveldb源码分析/">
            
                <a href="../leveldb源码分析/">
            
                    
                    leveldb源码分析
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.2.1" data-path="../leveldb源码分析/leveldb源码分析1.html">
            
                <a href="../leveldb源码分析/leveldb源码分析1.html">
            
                    
                    leveldb源码分析1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.2" data-path="../leveldb源码分析/leveldb源码分析2.html">
            
                <a href="../leveldb源码分析/leveldb源码分析2.html">
            
                    
                    leveldb源码分析2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.3" data-path="../leveldb源码分析/leveldb源码分析3.html">
            
                <a href="../leveldb源码分析/leveldb源码分析3.html">
            
                    
                    leveldb源码分析3
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.4" data-path="../leveldb源码分析/leveldb源码分析4.html">
            
                <a href="../leveldb源码分析/leveldb源码分析4.html">
            
                    
                    leveldb源码分析4
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.5" data-path="../leveldb源码分析/leveldb源码分析5.html">
            
                <a href="../leveldb源码分析/leveldb源码分析5.html">
            
                    
                    leveldb源码分析5
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.6" data-path="../leveldb源码分析/leveldb源码分析6.html">
            
                <a href="../leveldb源码分析/leveldb源码分析6.html">
            
                    
                    leveldb源码分析6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.7" data-path="../leveldb源码分析/leveldb源码分析7.html">
            
                <a href="../leveldb源码分析/leveldb源码分析7.html">
            
                    
                    leveldb源码分析7
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.8" data-path="../leveldb源码分析/leveldb源码分析8.html">
            
                <a href="../leveldb源码分析/leveldb源码分析8.html">
            
                    
                    leveldb源码分析8
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.9" data-path="../leveldb源码分析/leveldb源码分析9.html">
            
                <a href="../leveldb源码分析/leveldb源码分析9.html">
            
                    
                    leveldb源码分析9
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.10" data-path="../leveldb源码分析/leveldb源码分析10.html">
            
                <a href="../leveldb源码分析/leveldb源码分析10.html">
            
                    
                    leveldb源码分析10
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.11" data-path="../leveldb源码分析/leveldb源码分析11.html">
            
                <a href="../leveldb源码分析/leveldb源码分析11.html">
            
                    
                    leveldb源码分析11
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.12" data-path="../leveldb源码分析/leveldb源码分析12.html">
            
                <a href="../leveldb源码分析/leveldb源码分析12.html">
            
                    
                    leveldb源码分析12
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.13" data-path="../leveldb源码分析/leveldb源码分析13.html">
            
                <a href="../leveldb源码分析/leveldb源码分析13.html">
            
                    
                    leveldb源码分析13
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.14" data-path="../leveldb源码分析/leveldb源码分析14.html">
            
                <a href="../leveldb源码分析/leveldb源码分析14.html">
            
                    
                    leveldb源码分析14
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.15" data-path="../leveldb源码分析/leveldb源码分析15.html">
            
                <a href="../leveldb源码分析/leveldb源码分析15.html">
            
                    
                    leveldb源码分析15
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.16" data-path="../leveldb源码分析/leveldb源码分析16.html">
            
                <a href="../leveldb源码分析/leveldb源码分析16.html">
            
                    
                    leveldb源码分析16
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.17" data-path="../leveldb源码分析/leveldb源码分析17.html">
            
                <a href="../leveldb源码分析/leveldb源码分析17.html">
            
                    
                    leveldb源码分析17
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.18" data-path="../leveldb源码分析/leveldb源码分析18.html">
            
                <a href="../leveldb源码分析/leveldb源码分析18.html">
            
                    
                    leveldb源码分析18
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.19" data-path="../leveldb源码分析/leveldb源码分析19.html">
            
                <a href="../leveldb源码分析/leveldb源码分析19.html">
            
                    
                    leveldb源码分析19
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.20" data-path="../leveldb源码分析/leveldb源码分析20.html">
            
                <a href="../leveldb源码分析/leveldb源码分析20.html">
            
                    
                    leveldb源码分析20
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.21" data-path="../leveldb源码分析/leveldb源码分析21.html">
            
                <a href="../leveldb源码分析/leveldb源码分析21.html">
            
                    
                    leveldb源码分析21
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="7.2.22" data-path="../leveldb源码分析/leveldb源码分析22.html">
            
                <a href="../leveldb源码分析/leveldb源码分析22.html">
            
                    
                    leveldb源码分析22
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="8.1" data-path="../Memcached源码分析/">
            
                <a href="../Memcached源码分析/">
            
                    
                    Memcached源码分析
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1.1" data-path="../Memcached源码分析/00服务器资源调整.html">
            
                <a href="../Memcached源码分析/00服务器资源调整.html">
            
                    
                    00 服务器资源调整
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.2" data-path="../Memcached源码分析/01初始化参数解析.html">
            
                <a href="../Memcached源码分析/01初始化参数解析.html">
            
                    
                    01 初始化参数解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.3" data-path="../Memcached源码分析/02网络监听的建立.html">
            
                <a href="../Memcached源码分析/02网络监听的建立.html">
            
                    
                    02 网络监听的建立
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.4" data-path="../Memcached源码分析/03网络连接建立.html">
            
                <a href="../Memcached源码分析/03网络连接建立.html">
            
                    
                    03 网络连接建立
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.5" data-path="../Memcached源码分析/04内存初始化.html">
            
                <a href="../Memcached源码分析/04内存初始化.html">
            
                    
                    04 内存初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.6" data-path="../Memcached源码分析/05资源初始化.html">
            
                <a href="../Memcached源码分析/05资源初始化.html">
            
                    
                    05 资源初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.7" data-path="../Memcached源码分析/06get过程.html">
            
                <a href="../Memcached源码分析/06get过程.html">
            
                    
                    06 get过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.8" data-path="../Memcached源码分析/07cas属性.html">
            
                <a href="../Memcached源码分析/07cas属性.html">
            
                    
                    07 cas属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.9" data-path="../Memcached源码分析/08内存池.html">
            
                <a href="../Memcached源码分析/08内存池.html">
            
                    
                    08 内存池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.10" data-path="../Memcached源码分析/09连接队列.html">
            
                <a href="../Memcached源码分析/09连接队列.html">
            
                    
                    09 连接队列
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.11" data-path="../Memcached源码分析/10Hash表操作.html">
            
                <a href="../Memcached源码分析/10Hash表操作.html">
            
                    
                    10 Hash表操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.12" data-path="../Memcached源码分析/12set操作.html">
            
                <a href="../Memcached源码分析/12set操作.html">
            
                    
                    12 set操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.13" data-path="../Memcached源码分析/13do_item_alloc操作.html">
            
                <a href="../Memcached源码分析/13do_item_alloc操作.html">
            
                    
                    13 do_item_alloc操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.14" data-path="../Memcached源码分析/14item结构.html">
            
                <a href="../Memcached源码分析/14item结构.html">
            
                    
                    14 item结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.15" data-path="../Memcached源码分析/15Hash表扩容.html">
            
                <a href="../Memcached源码分析/15Hash表扩容.html">
            
                    
                    15 Hash表扩容
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.16" data-path="../Memcached源码分析/16线程交互.html">
            
                <a href="../Memcached源码分析/16线程交互.html">
            
                    
                    16 线程交互
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="8.1.17" data-path="../Memcached源码分析/17状态机.html">
            
                <a href="../Memcached源码分析/17状态机.html">
            
                    
                    17 状态机
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="divider"></li>
        
        
    
        <li class="chapter " data-level="9.1" data-path="../游戏开发专题/">
            
                <a href="../游戏开发专题/">
            
                    
                    游戏开发专题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1.1" data-path="../游戏开发专题/1游戏服务器开发的基本体系与服务器端开发的一些建议.html">
            
                <a href="../游戏开发专题/1游戏服务器开发的基本体系与服务器端开发的一些建议.html">
            
                    
                    1 游戏服务器开发的基本体系与服务器端开发的一些建议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.2" data-path="../游戏开发专题/2网络游戏服务器开发框架设计介绍.html">
            
                <a href="../游戏开发专题/2网络游戏服务器开发框架设计介绍.html">
            
                    
                    2 网络游戏服务器开发框架设计介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.3" data-path="../游戏开发专题/3游戏后端开发需要掌握的知识.html">
            
                <a href="../游戏开发专题/3游戏后端开发需要掌握的知识.html">
            
                    
                    3 游戏后端开发需要掌握的知识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.4" data-path="../游戏开发专题/4关于游戏服务端架构的整理.html">
            
                <a href="../游戏开发专题/4关于游戏服务端架构的整理.html">
            
                    
                    4 关于游戏服务端架构的整理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.5" data-path="../游戏开发专题/5各类游戏对应的服务端架构.html">
            
                <a href="../游戏开发专题/5各类游戏对应的服务端架构.html">
            
                    
                    5 各类游戏对应的服务端架构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.6" data-path="../游戏开发专题/6从腾讯QQgame高性能服务器集群架构看“分而治之”与“自治”等分布式架构设计原则.html">
            
                <a href="../游戏开发专题/6从腾讯QQgame高性能服务器集群架构看“分而治之”与“自治”等分布式架构设计原则.html">
            
                    
                    6 从腾讯QQgame高性能服务器集群架构看“分而治之”与“自治”等分布式架构设计原则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.7" data-path="../游戏开发专题/7QQ游戏百万人同时在线服务器架构实现.html">
            
                <a href="../游戏开发专题/7QQ游戏百万人同时在线服务器架构实现.html">
            
                    
                    7 QQ游戏百万人同时在线服务器架构实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.8" data-path="../游戏开发专题/8大型多人在线游戏服务器架构设计.html">
            
                <a href="../游戏开发专题/8大型多人在线游戏服务器架构设计.html">
            
                    
                    8 大型多人在线游戏服务器架构设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.9" data-path="../游戏开发专题/9百万用户级游戏服务器架构设计.html">
            
                <a href="../游戏开发专题/9百万用户级游戏服务器架构设计.html">
            
                    
                    9 百万用户级游戏服务器架构设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.10" data-path="../游戏开发专题/10十万在线的WebGame的数据库设计思路.html">
            
                <a href="../游戏开发专题/10十万在线的WebGame的数据库设计思路.html">
            
                    
                    10 十万在线的WebGame的数据库设计思路
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.11" data-path="../游戏开发专题/11一种高性能网络游戏服务器架构设计.html">
            
                <a href="../游戏开发专题/11一种高性能网络游戏服务器架构设计.html">
            
                    
                    11 一种高性能网络游戏服务器架构设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.12" data-path="../游戏开发专题/12经典游戏服务器端架构概述.html">
            
                <a href="../游戏开发专题/12经典游戏服务器端架构概述.html">
            
                    
                    12 经典游戏服务器端架构概述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="9.1.13" data-path="../游戏开发专题/13游戏跨服架构进化之路.html">
            
                <a href="../游戏开发专题/13游戏跨服架构进化之路.html">
            
                    
                    13 游戏跨服架构进化之路
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">Part IV</li>
        
        
    
        <li class="chapter " data-level="10.1" data-path="../程序员面试题精讲/">
            
                <a href="../程序员面试题精讲/">
            
                    
                    程序员面试题精讲
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.1.1" data-path="../程序员面试题精讲/腾讯后台开发实习生技能要求.html">
            
                <a href="../程序员面试题精讲/腾讯后台开发实习生技能要求.html">
            
                    
                    腾讯后台开发实习生技能要求
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.1.2" data-path="../程序员面试题精讲/聊聊如何拿大厂的offer.html">
            
                <a href="../程序员面试题精讲/聊聊如何拿大厂的offer.html">
            
                    
                    聊聊如何拿大厂的 offer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.1.3" data-path="../程序员面试题精讲/网络通信题目集锦.html">
            
                <a href="../程序员面试题精讲/网络通信题目集锦.html">
            
                    
                    网络通信题目集锦
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.1.4" data-path="../程序员面试题精讲/我面试后端开发经理的经历.html">
            
                <a href="../程序员面试题精讲/我面试后端开发经理的经历.html">
            
                    
                    我面试后端开发经理的经历
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.1.5" data-path="../程序员面试题精讲/LinuxCC++后端开发面试问哪些问题.html">
            
                <a href="../程序员面试题精讲/LinuxCC++后端开发面试问哪些问题.html">
            
                    
                    Linux C/C++后端开发面试问哪些问题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10.2" data-path="../职业规划/">
            
                <a href="../职业规划/">
            
                    
                    职业规划
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.2.1" data-path="../职业规划/给工作4年迷茫的程序员们的一点建议.html">
            
                <a href="../职业规划/给工作4年迷茫的程序员们的一点建议.html">
            
                    
                    给工作 4 年迷茫的程序员们的一点建议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.2.2" data-path="../职业规划/聊聊技术人员的常见的职业问题.html">
            
                <a href="../职业规划/聊聊技术人员的常见的职业问题.html">
            
                    
                    聊聊技术人员的常见的职业问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.2.3" data-path="../职业规划/写给那些傻傻想做服务器开发的朋友.html">
            
                <a href="../职业规划/写给那些傻傻想做服务器开发的朋友.html">
            
                    
                    写给那些傻傻想做服务器开发的朋友
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10.3" data-path="../自我提升与开源代码/">
            
                <a href="../自我提升与开源代码/">
            
                    
                    自我提升与开源代码
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.3.1" data-path="../自我提升与开源代码/2020年好好读一读开源代码吧.html">
            
                <a href="../自我提升与开源代码/2020年好好读一读开源代码吧.html">
            
                    
                    2020 年好好读一读开源代码吧
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10.4" data-path="../后端开发相关的书籍/">
            
                <a href="../后端开发相关的书籍/">
            
                    
                    后端开发相关的书籍
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.4.1" data-path="../后端开发相关的书籍/后台开发应该读的书.html">
            
                <a href="../后端开发相关的书籍/后台开发应该读的书.html">
            
                    
                    后台开发应该读的书
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10.5" data-path="../程序员的简历/">
            
                <a href="../程序员的简历/">
            
                    
                    程序员的简历
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.5.1" data-path="../程序员的简历/程序员如何写简历.html">
            
                <a href="../程序员的简历/程序员如何写简历.html">
            
                    
                    程序员如何写简历
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10.6" data-path="../程序员的薪资与年终奖那些事儿/">
            
                <a href="../程序员的薪资与年终奖那些事儿/">
            
                    
                    程序员的薪资与年终奖那些事儿
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.6.1" data-path="../程序员的薪资与年终奖那些事儿/技术面试与HR谈薪资技巧.html">
            
                <a href="../程序员的薪资与年终奖那些事儿/技术面试与HR谈薪资技巧.html">
            
                    
                    技术面试与HR谈薪资技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.6.2" data-path="../程序员的薪资与年终奖那些事儿/聊一聊程序员如何增加收入.html">
            
                <a href="../程序员的薪资与年终奖那些事儿/聊一聊程序员如何增加收入.html">
            
                    
                    聊一聊程序员如何增加收入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.6.3" data-path="../程序员的薪资与年终奖那些事儿/谈一谈年终奖.html">
            
                <a href="../程序员的薪资与年终奖那些事儿/谈一谈年终奖.html">
            
                    
                    谈一谈年终奖
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10.7" data-path="../程序员的烦心事/">
            
                <a href="../程序员的烦心事/">
            
                    
                    程序员的烦心事
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.7.1" data-path="../程序员的烦心事/拒绝了一家公司的offer后，他们的副总和hr总监同时打电话来询问拒绝原因并极力要求加入，我该不该去？.html">
            
                <a href="../程序员的烦心事/拒绝了一家公司的offer后，他们的副总和hr总监同时打电话来询问拒绝原因并极力要求加入，我该不该去？.html">
            
                    
                    拒绝了一家公司的offer后，他们的副总和hr总监同时打电话来询问拒绝原因并极力要求加入，我该不该去？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.7.2" data-path="../程序员的烦心事/我是一名程序员，结婚时女友要求我用两年的工资作为彩礼，我该不该答应？.html">
            
                <a href="../程序员的烦心事/我是一名程序员，结婚时女友要求我用两年的工资作为彩礼，我该不该答应？.html">
            
                    
                    我是一名程序员，结婚时女友要求我用两年的工资作为彩礼，我该不该答应？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10.8" data-path="../作者的故事/">
            
                <a href="../作者的故事/">
            
                    
                    作者的故事
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.8.1" data-path="../作者的故事/我的2019.html">
            
                <a href="../作者的故事/我的2019.html">
            
                    
                    我的 2019
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="10.8.2" data-path="../作者的故事/我是如何年薪五十万的.html">
            
                <a href="../作者的故事/我是如何年薪五十万的.html">
            
                    
                    我是如何年薪五十万的
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


		</nav>
		
		
	</div>

	<!-- Content nav -->
	<div class="book-anchor">
		<div class="book-anchor-title">在这篇文章中:</div>
		<div class="book-anchor-body">

		</div>
	</div>

	<div class="book-body">
		
		<div class="body-inner">
			
			

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Reactor模式</a>
    </h1>
</div>




			<div class="page-wrapper" tabindex="-1" role="main">
				<div class="page-inner">
					
<div id="book-search-results">
    <div class="search-noresults">
    
					<section class="normal markdown-section">
						
						<div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><li><span class="title-icon "></span><a href="#reactor&#x6A21;&#x5F0F;"><b></b>Reactor&#x6A21;&#x5F0F;</a></li></ul></ul></div><h2 id="reactor&#x6A21;&#x5F0F;"><a name="reactor&#x6A21;&#x5F0F;" class="anchor-navigation-ex-anchor" href="#reactor&#x6A21;&#x5F0F;"><i class="fa fa-link" aria-hidden="true"></i></a>Reactor&#x6A21;&#x5F0F;</h2>
<p>&#x6700;&#x8FD1;&#x4E00;&#x76F4;&#x5728;&#x770B;&#x6E38;&#x53CC;&#x7684;&#x300A;&#x9AD8;&#x6027;&#x80FD;Linux&#x670D;&#x52A1;&#x5668;&#x7F16;&#x7A0B;&#x300B;&#x4E00;&#x4E66;&#xFF0C;&#x4E0B;&#x8F7D;&#x94FE;&#x63A5;&#xFF1A; <a href="http://download.csdn.net/detail/analogous_love/9673008" target="_blank">http://download.csdn.net/detail/analogous_love/9673008</a></p>
<p>&#x4E66;&#x4E0A;&#x662F;&#x8FD9;&#x4E48;&#x4ECB;&#x7ECD;Reactor&#x6A21;&#x5F0F;&#x7684;&#xFF1A;</p>
<p><img src="../imgs/reactor1.jpg" alt=""></p>
<p>&#x6309;&#x7167;&#x8FD9;&#x4E2A;&#x601D;&#x8DEF;&#xFF0C;&#x6211;&#x5199;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x7EC3;&#x4E60;&#xFF1A;</p>
<pre class="language-"><code>/** 
 *@desc:   &#x7528;reactor&#x6A21;&#x5F0F;&#x7EC3;&#x4E60;&#x670D;&#x52A1;&#x5668;&#x7A0B;&#x5E8F;&#xFF0C;main.cpp
 *@author: zhangyl
 *@date:   2016.11.23
 */

#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>iostream</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>string.h</span><span class="token punctuation">&gt;</span></span>
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;arpa/inet.h&gt;  //for htonl() and htons()
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>unistd.h</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>fcntl.h</span><span class="token punctuation">&gt;</span></span>
#include &lt;sys/epoll.h&gt;
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>signal.h</span><span class="token punctuation">&gt;</span></span>     //for signal()
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>pthread.h</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>semaphore.h</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>list</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>errno.h</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>time.h</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>sstream</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>iomanip</span><span class="token punctuation">&gt;</span></span> //for std::setw()/setfill()
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>stdlib.h</span><span class="token punctuation">&gt;</span></span>


#define WORKER_THREAD_NUM   5

#define min(a, b) ((a &lt;= b) ? (a) : (b)) 

int g_epollfd = 0;
bool g_bStop = false;
int g_listenfd = 0;
pthread_t g_acceptthreadid = 0;
pthread_t g_threadid[WORKER_THREAD_NUM] = { 0 };
pthread_cond_t g_acceptcond;
pthread_mutex_t g_acceptmutex;

pthread_cond_t g_cond /*= PTHREAD_COND_INITIALIZER*/;
pthread_mutex_t g_mutex /*= PTHREAD_MUTEX_INITIALIZER*/;

pthread_mutex_t g_clientmutex;

std::list<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>int</span><span class="token punctuation">&gt;</span></span> g_listClients;

void prog_exit(int signo)
{
    ::signal(SIGINT, SIG_IGN);
    //::signal(SIGKILL, SIG_IGN);//&#x8BE5;&#x4FE1;&#x53F7;&#x4E0D;&#x80FD;&#x88AB;&#x963B;&#x585E;&#x3001;&#x5904;&#x7406;&#x6216;&#x8005;&#x5FFD;&#x7565;
    ::signal(SIGTERM, SIG_IGN);

    std::cout &lt;&lt; &quot;program recv signal &quot; &lt;&lt; signo &lt;&lt; &quot; to exit.&quot; &lt;&lt; std::endl;

    g_bStop = true;

    ::epoll_ctl(g_epollfd, EPOLL_CTL_DEL, g_listenfd, NULL);

    //TODO: &#x662F;&#x5426;&#x9700;&#x8981;&#x5148;&#x8C03;&#x7528;shutdown()&#x4E00;&#x4E0B;&#xFF1F;
    ::shutdown(g_listenfd, SHUT_RDWR);
    ::close(g_listenfd);
    ::close(g_epollfd);

    ::pthread_cond_destroy(&amp;g_acceptcond);
    ::pthread_mutex_destroy(&amp;g_acceptmutex);

    ::pthread_cond_destroy(&amp;g_cond);
    ::pthread_mutex_destroy(&amp;g_mutex);

    ::pthread_mutex_destroy(&amp;g_clientmutex);
}

bool create_server_listener(const char* ip, short port)
{
    g_listenfd = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
    if (g_listenfd == -1)
        return false;

    int on = 1;
    ::setsockopt(g_listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&amp;on, sizeof(on));
    ::setsockopt(g_listenfd, SOL_SOCKET, SO_REUSEPORT, (char *)&amp;on, sizeof(on));

    struct sockaddr_in servaddr;
    memset(&amp;servaddr, 0, sizeof(servaddr)); 
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr(ip);
    servaddr.sin_port = htons(port);
    if (::bind(g_listenfd, (sockaddr *)&amp;servaddr, sizeof(servaddr)) == -1)
        return false;

    if (::listen(g_listenfd, 50) == -1)
        return false;

    g_epollfd = ::epoll_create(1);
    if (g_epollfd == -1)
        return false;

    struct epoll_event e;
    memset(&amp;e, 0, sizeof(e));
    e.events = EPOLLIN | EPOLLRDHUP;
    e.data.fd = g_listenfd;
    if (::epoll_ctl(g_epollfd, EPOLL_CTL_ADD, g_listenfd, &amp;e) == -1)
        return false;

    return true;
}

void release_client(int clientfd)
{
    if (::epoll_ctl(g_epollfd, EPOLL_CTL_DEL, clientfd, NULL) == -1)
        std::cout &lt;&lt; &quot;release client socket failed as call epoll_ctl failed&quot; &lt;&lt; std::endl;

    ::close(clientfd);
}

void* accept_thread_func(void* arg)
{   
    while (!g_bStop)
    {
        ::pthread_mutex_lock(&amp;g_acceptmutex);
        ::pthread_cond_wait(&amp;g_acceptcond, &amp;g_acceptmutex);
        //::pthread_mutex_lock(&amp;g_acceptmutex);

        //std::cout &lt;&lt; &quot;run loop in accept_thread_func&quot; &lt;&lt; std::endl;

        struct sockaddr_in clientaddr;
        socklen_t addrlen;
        int newfd = ::accept(g_listenfd, (struct sockaddr *)&amp;clientaddr, &amp;addrlen);
        ::pthread_mutex_unlock(&amp;g_acceptmutex);
        if (newfd == -1)
            continue;

        std::cout &lt;&lt; &quot;new client connected: &quot; &lt;&lt; ::inet_ntoa(clientaddr.sin_addr) &lt;&lt; &quot;:&quot; &lt;&lt; ::ntohs(clientaddr.sin_port) &lt;&lt; std::endl;

        //&#x5C06;&#x65B0;socket&#x8BBE;&#x7F6E;&#x4E3A;non-blocking
        int oldflag = ::fcntl(newfd, F_GETFL, 0);
        int newflag = oldflag | O_NONBLOCK;
        if (::fcntl(newfd, F_SETFL, newflag) == -1)
        {
            std::cout &lt;&lt; &quot;fcntl error, oldflag =&quot; &lt;&lt; oldflag &lt;&lt; &quot;, newflag = &quot; &lt;&lt; newflag &lt;&lt; std::endl;
            continue;
        }

        struct epoll_event e;
        memset(&amp;e, 0, sizeof(e));
        e.events = EPOLLIN | EPOLLRDHUP | EPOLLET;
        e.data.fd = newfd;
        if (::epoll_ctl(g_epollfd, EPOLL_CTL_ADD, newfd, &amp;e) == -1)
        {
            std::cout &lt;&lt; &quot;epoll_ctl error, fd =&quot; &lt;&lt; newfd &lt;&lt; std::endl;
        }
    }

    return NULL;
}

void* worker_thread_func(void* arg)
{   
    while (!g_bStop)
    {
        int clientfd;
        ::pthread_mutex_lock(&amp;g_clientmutex);
        while (g_listClients.empty())
            ::pthread_cond_wait(&amp;g_cond, &amp;g_clientmutex);
        clientfd = g_listClients.front();
        g_listClients.pop_front();  
        pthread_mutex_unlock(&amp;g_clientmutex);

        //gdb&#x8C03;&#x8BD5;&#x65F6;&#x4E0D;&#x80FD;&#x5B9E;&#x65F6;&#x5237;&#x65B0;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#xFF0C;&#x7528;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x5237;&#x65B0;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#xFF0C;&#x4F7F;&#x4FE1;&#x606F;&#x5728;&#x5C4F;&#x5E55;&#x4E0A;&#x5B9E;&#x65F6;&#x663E;&#x793A;&#x51FA;&#x6765;
        std::cout &lt;&lt; std::endl;

        std::string strclientmsg;
        char buff[256];
        bool bError = false;
        while (true)
        {
            memset(buff, 0, sizeof(buff));
            int nRecv = ::recv(clientfd, buff, 256, 0);
            if (nRecv == -1)
            {
                if (errno == EWOULDBLOCK)
                    break;
                else
                {
                    std::cout &lt;&lt; &quot;recv error, client disconnected, fd = &quot; &lt;&lt; clientfd &lt;&lt; std::endl;
                    release_client(clientfd);
                    bError = true;
                    break;
                }

            }
            //&#x5BF9;&#x7AEF;&#x5173;&#x95ED;&#x4E86;socket&#xFF0C;&#x8FD9;&#x7AEF;&#x4E5F;&#x5173;&#x95ED;&#x3002;
            else if (nRecv == 0)
            {
                std::cout &lt;&lt; &quot;peer closed, client disconnected, fd = &quot; &lt;&lt; clientfd &lt;&lt; std::endl;
                release_client(clientfd);
                bError = true;
                break;
            }

            strclientmsg += buff;
        }

        //&#x51FA;&#x9519;&#x4E86;&#xFF0C;&#x5C31;&#x4E0D;&#x8981;&#x518D;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x6267;&#x884C;&#x4E86;
        if (bError)
            continue;

        std::cout &lt;&lt; &quot;client msg: &quot; &lt;&lt; strclientmsg;

        //&#x5C06;&#x6D88;&#x606F;&#x52A0;&#x4E0A;&#x65F6;&#x95F4;&#x6807;&#x7B7E;&#x540E;&#x53D1;&#x56DE;
        time_t now = time(NULL);
        struct tm* nowstr = localtime(&amp;now);
        std::ostringstream ostimestr;
        ostimestr &lt;&lt; &quot;[&quot; &lt;&lt; nowstr-&gt;tm_year + 1900 &lt;&lt; &quot;-&quot; 
                  &lt;&lt; std::setw(2) &lt;&lt; std::setfill(&apos;0&apos;) &lt;&lt; nowstr-&gt;tm_mon + 1 &lt;&lt; &quot;-&quot; 
                  &lt;&lt; std::setw(2) &lt;&lt; std::setfill(&apos;0&apos;) &lt;&lt; nowstr-&gt;tm_mday &lt;&lt; &quot; &quot;
                  &lt;&lt; std::setw(2) &lt;&lt; std::setfill(&apos;0&apos;) &lt;&lt; nowstr-&gt;tm_hour &lt;&lt; &quot;:&quot; 
                  &lt;&lt; std::setw(2) &lt;&lt; std::setfill(&apos;0&apos;) &lt;&lt; nowstr-&gt;tm_min &lt;&lt; &quot;:&quot; 
                  &lt;&lt; std::setw(2) &lt;&lt; std::setfill(&apos;0&apos;) &lt;&lt; nowstr-&gt;tm_sec &lt;&lt; &quot;]server reply: &quot;;

        strclientmsg.insert(0, ostimestr.str());

        while (true)
        {
            int nSent = ::send(clientfd, strclientmsg.c_str(), strclientmsg.length(), 0);
            if (nSent == -1)
            {
                if (errno == EWOULDBLOCK)
                {
                    ::sleep(10);
                    continue;
                }
                else
                {
                    std::cout &lt;&lt; &quot;send error, fd = &quot; &lt;&lt; clientfd &lt;&lt; std::endl;
                    release_client(clientfd);
                    break;
                }

            }          

            std::cout &lt;&lt; &quot;send: &quot; &lt;&lt; strclientmsg;
            strclientmsg.erase(0, nSent);

            if (strclientmsg.empty())
                break;
        }
    }

    return NULL;
}

void daemon_run()
{
    int pid;
    signal(SIGCHLD, SIG_IGN);
    //1&#xFF09;&#x5728;&#x7236;&#x8FDB;&#x7A0B;&#x4E2D;&#xFF0C;fork&#x8FD4;&#x56DE;&#x65B0;&#x521B;&#x5EFA;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x8FDB;&#x7A0B;ID&#xFF1B;
    //2&#xFF09;&#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#xFF0C;fork&#x8FD4;&#x56DE;0&#xFF1B;
    //3&#xFF09;&#x5982;&#x679C;&#x51FA;&#x73B0;&#x9519;&#x8BEF;&#xFF0C;fork&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x8D1F;&#x503C;&#xFF1B;
    pid = fork();
    if (pid &lt; 0)
    {
        std:: cout &lt;&lt; &quot;fork error&quot; &lt;&lt; std::endl;
        exit(-1);
    }
    //&#x7236;&#x8FDB;&#x7A0B;&#x9000;&#x51FA;&#xFF0C;&#x5B50;&#x8FDB;&#x7A0B;&#x72EC;&#x7ACB;&#x8FD0;&#x884C;
    else if (pid &gt; 0) {
        exit(0);
    }
    //&#x4E4B;&#x524D;parent&#x548C;child&#x8FD0;&#x884C;&#x5728;&#x540C;&#x4E00;&#x4E2A;session&#x91CC;,parent&#x662F;&#x4F1A;&#x8BDD;&#xFF08;session&#xFF09;&#x7684;&#x9886;&#x5934;&#x8FDB;&#x7A0B;,
    //parent&#x8FDB;&#x7A0B;&#x4F5C;&#x4E3A;&#x4F1A;&#x8BDD;&#x7684;&#x9886;&#x5934;&#x8FDB;&#x7A0B;&#xFF0C;&#x5982;&#x679C;exit&#x7ED3;&#x675F;&#x6267;&#x884C;&#x7684;&#x8BDD;&#xFF0C;&#x90A3;&#x4E48;&#x5B50;&#x8FDB;&#x7A0B;&#x4F1A;&#x6210;&#x4E3A;&#x5B64;&#x513F;&#x8FDB;&#x7A0B;&#xFF0C;&#x5E76;&#x88AB;init&#x6536;&#x517B;&#x3002;
    //&#x6267;&#x884C;setsid()&#x4E4B;&#x540E;,child&#x5C06;&#x91CD;&#x65B0;&#x83B7;&#x5F97;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x4F1A;&#x8BDD;(session)id&#x3002;
    //&#x8FD9;&#x65F6;parent&#x9000;&#x51FA;&#x4E4B;&#x540E;,&#x5C06;&#x4E0D;&#x4F1A;&#x5F71;&#x54CD;&#x5230;child&#x4E86;&#x3002;
    setsid();
    int fd;
    fd = open(&quot;/dev/null&quot;, O_RDWR, 0);
    if (fd != -1)
    {
        dup2(fd, STDIN_FILENO);
        dup2(fd, STDOUT_FILENO);
        dup2(fd, STDERR_FILENO);
    }
    if (fd &gt; 2)
        close(fd);

}


int main(int argc, char* argv[])
{  
    short port = 0;
    int ch;
    bool bdaemon = false;
    while ((ch = getopt(argc, argv, &quot;p:d&quot;)) != -1)
    {
        switch (ch)
        {
        case &apos;d&apos;:
            bdaemon = true;
            break;
        case &apos;p&apos;:
            port = atol(optarg);
            break;
        }
    }

    if (bdaemon)
        daemon_run();


    if (port == 0)
        port = 12345;

    if (!create_server_listener(&quot;0.0.0.0&quot;, port))
    {
        std::cout &lt;&lt; &quot;Unable to create listen server: ip=0.0.0.0, port=&quot; &lt;&lt; port &lt;&lt; &quot;.&quot; &lt;&lt; std::endl;
        return -1;
    }


    //&#x8BBE;&#x7F6E;&#x4FE1;&#x53F7;&#x5904;&#x7406;
    signal(SIGCHLD, SIG_DFL);
    signal(SIGPIPE, SIG_IGN);
    signal(SIGINT, prog_exit);
    //signal(SIGKILL, prog_exit);//&#x8BE5;&#x4FE1;&#x53F7;&#x4E0D;&#x80FD;&#x88AB;&#x963B;&#x585E;&#x3001;&#x5904;&#x7406;&#x6216;&#x8005;&#x5FFD;&#x7565;

    signal(SIGTERM, prog_exit);

    ::pthread_cond_init(&amp;g_acceptcond, NULL);
    ::pthread_mutex_init(&amp;g_acceptmutex, NULL);

    ::pthread_cond_init(&amp;g_cond, NULL);
    ::pthread_mutex_init(&amp;g_mutex, NULL);

    ::pthread_mutex_init(&amp;g_clientmutex, NULL);

    ::pthread_create(&amp;g_acceptthreadid, NULL, accept_thread_func, NULL);
    //&#x542F;&#x52A8;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;
    for (int i = 0; i &lt; WORKER_THREAD_NUM; ++i)
    {
        ::pthread_create(&amp;g_threadid[i], NULL, worker_thread_func, NULL);
    }

    while (!g_bStop)
    {       
        struct epoll_event ev[1024];
        int n = ::epoll_wait(g_epollfd, ev, 1024, 10);
        if (n == 0)
            continue;
        else if (n &lt; 0)
        {
            std::cout &lt;&lt; &quot;epoll_wait error&quot; &lt;&lt; std::endl;
            continue;
        }

        int m = min(n, 1024);
        for (int i = 0; i &lt; m; ++i)
        {
            //&#x901A;&#x77E5;&#x63A5;&#x6536;&#x8FDE;&#x63A5;&#x7EBF;&#x7A0B;&#x63A5;&#x6536;&#x65B0;&#x8FDE;&#x63A5;
            if (ev[i].data.fd == g_listenfd)
                pthread_cond_signal(&amp;g_acceptcond);
            //&#x901A;&#x77E5;&#x666E;&#x901A;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x63A5;&#x6536;&#x6570;&#x636E;
            else
            {               
                pthread_mutex_lock(&amp;g_clientmutex);              
                g_listClients.push_back(ev[i].data.fd);
                pthread_mutex_unlock(&amp;g_clientmutex);
                pthread_cond_signal(&amp;g_cond);
                //std::cout &lt;&lt; &quot;signal&quot; &lt;&lt; std::endl;
            }

        }

    }

    return 0;
}
</code></pre><p>&#x7A0B;&#x5E8F;&#x7684;&#x529F;&#x80FD;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;echo&#x670D;&#x52A1;&#xFF1A;&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x4E0A;&#x670D;&#x52A1;&#x5668;&#x4E4B;&#x540E;&#xFF0C;&#x7ED9;&#x670D;&#x52A1;&#x5668;&#x53D1;&#x9001;&#x4FE1;&#x606F;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x52A0;&#x4E0A;&#x65F6;&#x95F4;&#x6233;&#x7B49;&#x4FE1;&#x606F;&#x540E;&#x8FD4;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x3002;</p>
<p>&#x4F7F;&#x7528;&#x5230;&#x7684;&#x77E5;&#x8BC6;&#x70B9;&#x6709;&#xFF1A;</p>
<ol>
<li><p>&#x6761;&#x4EF6;&#x53D8;&#x91CF;</p>
</li>
<li><p>epoll&#x7684;&#x8FB9;&#x7F18;&#x89E6;&#x53D1;&#x6A21;&#x5F0F;</p>
</li>
</ol>
<p>&#x7A0B;&#x5E8F;&#x7684;&#x5927;&#x81F4;&#x6846;&#x67B6;&#x662F;&#xFF1A;</p>
<ol>
<li>&#x4E3B;&#x7EBF;&#x7A0B;&#x53EA;&#x8D1F;&#x8D23;&#x76D1;&#x542C;&#x4FA6;&#x542C;socket&#x4E0A;&#x662F;&#x5426;&#x6709;&#x65B0;&#x8FDE;&#x63A5;&#xFF0C;&#x5982;&#x679C;&#x6709;&#x65B0;&#x8FDE;&#x63A5;&#x5230;&#x6765;&#xFF0C;&#x4EA4;&#x7ED9;&#x4E00;&#x4E2A;&#x53EB;accept&#x7684;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x53BB;&#x63A5;&#x6536;&#x65B0;&#x8FDE;&#x63A5;&#xFF0C;&#x5E76;&#x5C06;&#x65B0;&#x8FDE;&#x63A5;socket&#x7ED1;&#x5B9A;&#x5230;&#x4E3B;&#x7EBF;&#x7A0B;&#x4F7F;&#x7528;epollfd&#x4E0A;&#x53BB;&#x3002;</li>
<li>&#x4E3B;&#x7EBF;&#x7A0B;&#x5982;&#x679C;&#x4FA6;&#x542C;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#x7684;socket&#x4E0A;&#x6709;&#x53EF;&#x8BFB;&#x4E8B;&#x4EF6;&#xFF0C;&#x5219;&#x901A;&#x77E5;&#x53E6;&#x5916;&#x4E94;&#x4E2A;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x53BB;&#x63A5;&#x6536;&#x5904;&#x7406;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x6765;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x5C06;&#x6570;&#x636E;&#x52A0;&#x4E0A;&#x65F6;&#x95F4;&#x6233;&#x540E;&#x53D1;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x3002;</li>
<li>&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4F20;&#x9012;-p port&#x6765;&#x8BBE;&#x7F6E;&#x7A0B;&#x5E8F;&#x7684;&#x76D1;&#x542C;&#x7AEF;&#x53E3;&#x53F7;&#xFF1B;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4F20;&#x9012;-d&#x6765;&#x4F7F;&#x7A0B;&#x5E8F;&#x4EE5;daemon&#x6A21;&#x5F0F;&#x8FD0;&#x884C;&#x5728;&#x540E;&#x53F0;&#x3002;&#x8FD9;&#x4E5F;&#x662F;&#x6807;&#x51C6;linux daemon&#x6A21;&#x5F0F;&#x7684;&#x4E66;&#x5199;&#x65B9;&#x6CD5;&#x3002;</li>
</ol>
<p>&#x7A0B;&#x5E8F;&#x96BE;&#x70B9;&#x548C;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x5730;&#x65B9;&#x662F;&#xFF1A;</p>
<ol>
<li>&#x6761;&#x4EF6;&#x53D8;&#x91CF;&#x4E3A;&#x4E86;&#x9632;&#x6B62;&#x865A;&#x5047;&#x5524;&#x9192;&#xFF0C;&#x4E00;&#x5B9A;&#x8981;&#x5728;&#x4E00;&#x4E2A;&#x5FAA;&#x73AF;&#x91CC;&#x9762;&#x8C03;&#x7528;pthread_cond_wait()&#x51FD;&#x6570;&#xFF0C;&#x6211;&#x5728;worker_thread_func()&#x4E2D;&#x4F7F;&#x7528;&#x4E86;&#xFF1A;</li>
</ol>
<pre class="language-"><code>while (g_listClients.empty())  
    ::pthread_cond_wait(&amp;g_cond, &amp;g_clientmutex);
</code></pre><p>&#x5728;accept_thread_func()&#x51FD;&#x6570;&#x91CC;&#x9762;&#x6211;&#x6CA1;&#x6709;&#x4F7F;&#x7528;&#x5FAA;&#x73AF;&#xFF0C;&#x8FD9;&#x6837;&#x4F1A;&#x6709;&#x95EE;&#x9898;&#x5417;&#xFF1F;</p>
<ol>
<li>&#x4F7F;&#x7528;&#x6761;&#x4EF6;&#x53D8;&#x91CF;pthread_cond_wait()&#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#x4E00;&#x5B9A;&#x8981;&#x5148;&#x83B7;&#x5F97;&#x4E0E;&#x8BE5;&#x6761;&#x4EF6;&#x53D8;&#x91CF;&#x76F8;&#x5173;&#x7684;mutex&#xFF0C;&#x5373;&#x50CF;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x7684;&#x7ED3;&#x6784;&#xFF1A;</li>
</ol>
<pre class="language-"><code>mutex_lock(...);

while (condition is true)
    ::pthread_cond_wait(...);

//&#x8FD9;&#x91CC;&#x53EF;&#x4EE5;&#x6709;&#x5176;&#x4ED6;&#x4EE3;&#x7801;...
mutex_unlock(...);

//&#x8FD9;&#x91CC;&#x53EF;&#x4EE5;&#x6709;&#x5176;&#x4ED6;&#x4EE3;&#x7801;...
</code></pre><p>&#x56E0;&#x4E3A;pthread_cond_wait()&#x5982;&#x679C;&#x963B;&#x585E;&#x7684;&#x8BDD;&#xFF0C;&#x5B83;&#x89E3;&#x9501;&#x76F8;&#x5173;mutex&#x548C;&#x963B;&#x585E;&#x5F53;&#x524D;&#x7EBF;&#x7A0B;&#x8FD9;&#x4E24;&#x4E2A;&#x52A8;&#x4F5C;&#x52A0;&#x5728;&#x4E00;&#x8D77;&#x662F;&#x539F;&#x5B50;&#x7684;&#x3002;</p>
<ol>
<li>&#x4F5C;&#x4E3A;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x7A0B;&#x5E8F;&#x6700;&#x597D;&#x5BF9;&#x4FA6;&#x542C;socket&#x8C03;&#x7528;setsocketopt()&#x8BBE;&#x7F6E;SO_REUSEADDR&#x548C;SO_REUSEPORT&#x4E24;&#x4E2A;&#x6807;&#x5FD7;&#xFF0C;&#x56E0;&#x4E3A;&#x670D;&#x52A1;&#x7A0B;&#x5E8F;&#x6709;&#x65F6;&#x5019;&#x4F1A;&#x9700;&#x8981;&#x91CD;&#x542F;&#xFF08;&#x6BD4;&#x5982;&#x8C03;&#x8BD5;&#x7684;&#x65F6;&#x5019;&#x5C31;&#x4F1A;&#x4E0D;&#x65AD;&#x91CD;&#x542F;&#xFF09;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x8BBE;&#x7F6E;&#x8FD9;&#x4E24;&#x4E2A;&#x6807;&#x5FD7;&#x7684;&#x8BDD;&#xFF0C;&#x7ED1;&#x5B9A;&#x7AEF;&#x53E3;&#x65F6;&#x5C31;&#x4F1A;&#x8C03;&#x7528;&#x5931;&#x8D25;&#x3002;&#x56E0;&#x4E3A;&#x4E00;&#x4E2A;&#x7AEF;&#x53E3;&#x4F7F;&#x7528;&#x540E;&#xFF0C;&#x5373;&#x4F7F;&#x4E0D;&#x518D;&#x4F7F;&#x7528;&#xFF0C;&#x56E0;&#x4E3A;&#x56DB;&#x6B21;&#x6325;&#x624B;&#x8BE5;&#x7AEF;&#x53E3;&#x5904;&#x4E8E;TIME_WAIT&#x72B6;&#x6001;&#xFF0C;&#x6709;&#x5927;&#x7EA6;2min&#x7684;MSL&#xFF08;Maximum Segment Lifetime&#xFF0C;&#x6700;&#x5927;&#x5B58;&#x6D3B;&#x671F;&#xFF09;&#x3002;&#x8FD9;2min&#x5185;&#xFF0C;&#x8BE5;&#x7AEF;&#x53E3;&#x662F;&#x4E0D;&#x80FD;&#x88AB;&#x91CD;&#x590D;&#x4F7F;&#x7528;&#x7684;&#x3002;&#x4F60;&#x7684;&#x670D;&#x52A1;&#x5668;&#x7A0B;&#x5E8F;&#x4E0A;&#x6B21;&#x4F7F;&#x7528;&#x4E86;&#x8FD9;&#x4E2A;&#x7AEF;&#x53E3;&#x53F7;&#xFF0C;&#x63A5;&#x7740;&#x91CD;&#x542F;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x4E2A;&#x7F18;&#x6545;&#xFF0C;&#x4F60;&#x518D;&#x6B21;&#x7ED1;&#x5B9A;&#x8FD9;&#x4E2A;&#x7AEF;&#x53E3;&#x5C31;&#x4F1A;&#x5931;&#x8D25;&#xFF08;bind&#x51FD;&#x6570;&#x8C03;&#x7528;&#x5931;&#x8D25;&#xFF09;&#x3002;&#x8981;&#x4E0D;&#x4F60;&#x5C31;&#x6BCF;&#x6B21;&#x91CD;&#x542F;&#x65F6;&#x9700;&#x8981;&#x7B49;&#x5F85;2min&#x540E;&#x518D;&#x8BD5;&#xFF08;&#x8FD9;&#x5728;&#x9891;&#x7E41;&#x91CD;&#x542F;&#x7A0B;&#x5E8F;&#x8C03;&#x8BD5;&#x662F;&#x96BE;&#x4EE5;&#x63A5;&#x6536;&#x7684;&#xFF09;&#xFF0C;&#x6216;&#x8005;&#x8BBE;&#x7F6E;&#x8FD9;&#x79CD;SO_REUSEADDR&#x548C;SO_REUSEPORT&#x7ACB;&#x5373;&#x56DE;&#x6536;&#x7AEF;&#x53E3;&#x4F7F;&#x7528;&#x3002;</li>
</ol>
<p>&#x5176;&#x5B9E;&#xFF0C;SO_REUSEADDR&#x5728;Windows&#x4E0A;&#x548C;Unix&#x5E73;&#x53F0;&#x4E0A;&#x8FD8;&#x6709;&#x4E9B;&#x7EC6;&#x5FAE;&#x7684;&#x533A;&#x522B;&#xFF0C;&#x6211;&#x5728;libevent&#x6E90;&#x7801;&#x4E2D;&#x770B;&#x5230;&#x8FD9;&#x6837;&#x7684;&#x63CF;&#x8FF0;&#xFF1A;</p>
<pre class="language-"><code>int evutil_make_listen_socket_reuseable(evutil_socket_t sock)
{
#ifndef WIN32
    int one = 1;
    /* REUSEADDR on Unix means, &quot;don&apos;t hang on to this address after the
     * listener is closed.&quot;  On Windows, though, it means &quot;don&apos;t keep other
     * processes from binding to this address while we&apos;re using it. */
    return setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*) &amp;one,
        (ev_socklen_t)sizeof(one));
#else
    return 0;
#endif
}
</code></pre><p>&#x6CE8;&#x610F;&#x6CE8;&#x91CA;&#x90E8;&#x5206;&#xFF0C;&#x5728;Unix&#x5E73;&#x53F0;&#x4E0A;&#x8BBE;&#x7F6E;&#x8FD9;&#x4E2A;&#x9009;&#x9879;&#x610F;&#x5473;&#x7740;&#xFF0C;&#x4EFB;&#x610F;&#x8FDB;&#x7A0B;&#x53EF;&#x4EE5;&#x590D;&#x7528;&#x8BE5;&#x5730;&#x5740;&#xFF1B;&#x800C;&#x5728;windows&#xFF0C;&#x4E0D;&#x8981;&#x963B;&#x6B62;&#x5176;&#x4ED6;&#x8FDB;&#x7A0B;&#x590D;&#x7528;&#x8BE5;&#x5730;&#x5740;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x5728;&#x5728;Unix&#x5E73;&#x53F0;&#x4E0A;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x8BBE;&#x7F6E;&#x8FD9;&#x4E2A;&#x9009;&#x9879;&#xFF0C;&#x4EFB;&#x610F;&#x8FDB;&#x7A0B;&#x5728;&#x4E00;&#x5B9A;&#x65F6;&#x95F4;&#x5185;&#xFF0C;&#x4E0D;&#x80FD;bind&#x8BE5;&#x5730;&#x5740;&#xFF1B;&#x5728;windows&#x5E73;&#x53F0;&#x4E0A;&#xFF0C;&#x5728;&#x4E00;&#x5B9A;&#x65F6;&#x95F4;&#x5185;&#xFF0C;&#x5176;&#x4ED6;&#x8FDB;&#x7A0B;&#x4E0D;&#x80FD;bind&#x8BE5;&#x5730;&#x5740;&#xFF0C;&#x800C;&#x672C;&#x8FDB;&#x7A0B;&#x5374;&#x53EF;&#x4EE5;&#x518D;&#x6B21;bind&#x8BE5;&#x5730;&#x5740;&#x3002;</p>
<ol>
<li>epoll_wait&#x5BF9;&#x65B0;&#x8FDE;&#x63A5;socket&#x4F7F;&#x7528;&#x7684;&#x662F;&#x8FB9;&#x7F18;&#x89E6;&#x53D1;&#x6A21;&#x5F0F;EPOLLET&#xFF08;edge trigger&#xFF09;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x9ED8;&#x8BA4;&#x7684;&#x6C34;&#x5E73;&#x89E6;&#x53D1;&#x6A21;&#x5F0F;&#xFF08;level trigger)&#x3002;&#x56E0;&#x4E3A;&#x5982;&#x679C;&#x91C7;&#x53D6;&#x6C34;&#x5E73;&#x89E6;&#x53D1;&#x6A21;&#x5F0F;&#x7684;&#x8BDD;&#xFF0C;&#x4E3B;&#x7EBF;&#x7A0B;&#x68C0;&#x6D4B;&#x5230;&#x67D0;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;socket&#x6570;&#x636E;&#x53EF;&#x8BFB;&#x65F6;&#xFF0C;&#x901A;&#x77E5;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x53BB;&#x6536;&#x53D6;&#x8BE5;socket&#x4E0A;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x4E3B;&#x7EBF;&#x7A0B;&#x7EE7;&#x7EED;&#x5FAA;&#x73AF;&#xFF0C;&#x53EA;&#x8981;&#x5728;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x6CA1;&#x6709;&#x5C06;&#x8BE5;socket&#x4E0A;&#x6570;&#x636E;&#x5168;&#x90E8;&#x6536;&#x5B8C;&#xFF0C;&#x6216;&#x8005;&#x5728;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x6536;&#x53D6;&#x6570;&#x636E;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x6709;&#x65B0;&#x6570;&#x636E;&#x5230;&#x6765;&#xFF0C;&#x4E3B;&#x7EBF;&#x7A0B;&#x4F1A;&#x7EE7;&#x7EED;&#x53D1;&#x901A;&#x77E5;&#xFF08;&#x901A;&#x8FC7;pthread_cond_signal()&#xFF09;&#x51FD;&#x6570;&#xFF0C;&#x518D;&#x6B21;&#x901A;&#x77E5;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x6536;&#x53D6;&#x6570;&#x636E;&#x3002;&#x8FD9;&#x6837;&#x4F1A;&#x53EF;&#x80FD;&#x5BFC;&#x81F4;&#x591A;&#x4E2A;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x540C;&#x65F6;&#x8C03;&#x7528;recv&#x51FD;&#x6570;&#x6536;&#x53D6;&#x8BE5;&#x5BA2;&#x6237;&#x7AEF;socket&#x4E0A;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x8FD9;&#x6837;&#x4EA7;&#x751F;&#x7684;&#x7ED3;&#x679C;&#x5C06;&#x4F1A;&#x5BFC;&#x81F4;&#x6570;&#x636E;&#x9519;&#x4E71;&#x3002;</li>
</ol>
<p>&#x76F8;&#x53CD;&#xFF0C;&#x91C7;&#x53D6;&#x8FB9;&#x7F18;&#x89E6;&#x53D1;&#x6A21;&#x5F0F;&#xFF0C;&#x53EA;&#x6709;&#x7B49;&#x67D0;&#x4E2A;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x5C06;&#x90A3;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;socket&#x4E0A;&#x6570;&#x636E;&#x5168;&#x90E8;&#x6536;&#x53D6;&#x5B8C;&#x6BD5;&#xFF0C;&#x4E3B;&#x7EBF;&#x7A0B;&#x7684;epoll_wait&#x624D;&#x53EF;&#x80FD;&#x4F1A;&#x518D;&#x6B21;&#x89E6;&#x53D1;&#x6765;&#x901A;&#x77E5;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x7EE7;&#x7EED;&#x6536;&#x53D6;&#x90A3;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;socket&#x65B0;&#x6765;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<ol>
<li><p>&#x4EE3;&#x7801;&#x4E2D;&#x6709;&#x8FD9;&#x6837;&#x4E00;&#x884C;&#xFF1A;</p>
<pre class="language-"><code> //gdb&#x8C03;&#x8BD5;&#x65F6;&#x4E0D;&#x80FD;&#x5B9E;&#x65F6;&#x5237;&#x65B0;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#xFF0C;&#x7528;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x5237;&#x65B0;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#xFF0C;&#x4F7F;&#x4FE1;&#x606F;&#x5728;&#x5C4F;&#x5E55;&#x4E0A;&#x5B9E;&#x65F6;&#x663E;&#x793A;&#x51FA;&#x6765; 
 std::cout &lt;&lt; std::endl;
</code></pre></li>
</ol>
<p>&#x5982;&#x679C;&#x4E0D;&#x52A0;&#x4E0A;&#x8FD9;&#x4E00;&#x884C;&#xFF0C;&#x6B63;&#x5E38;&#x8FD0;&#x884C;&#x670D;&#x52A1;&#x5668;&#x7A0B;&#x5E8F;&#xFF0C;&#x7A0B;&#x5E8F;&#x4E2D;&#x8981;&#x6253;&#x5370;&#x5230;&#x63A7;&#x5236;&#x53F0;&#x7684;&#x4FE1;&#x606F;&#x90FD;&#x4F1A;&#x6253;&#x5370;&#x51FA;&#x6765;&#xFF0C;&#x4F46;&#x662F;&#x5982;&#x679C;&#x7528;gdb&#x8C03;&#x8BD5;&#x72B6;&#x6001;&#x4E0B;&#xFF0C;&#x7A0B;&#x5E8F;&#x7684;&#x6240;&#x6709;&#x8F93;&#x51FA;&#x5C31;&#x4E0D;&#x663E;&#x793A;&#x4E86;&#x3002;&#x6211;&#x4E0D;&#x77E5;&#x9053;&#x8FD9;&#x662F;&#x4E0D;&#x662F;gdb&#x7684;&#x4E00;&#x4E2A;bug&#xFF0C;&#x6240;&#x4EE5;&#x8FD9;&#x91CC;&#x52A0;&#x4E0A;std::endl&#x6765;&#x8F93;&#x51FA;&#x4E00;&#x4E2A;&#x6362;&#x884C;&#x7B26;&#x5E76;flush&#x6807;&#x51C6;&#x8F93;&#x51FA;&#xFF0C;&#x8BA9;&#x8F93;&#x51FA;&#x663E;&#x793A;&#x51FA;&#x6765;&#x3002;&#xFF08;std::endl&#x4E0D;&#x4EC5;&#x662F;&#x8F93;&#x51FA;&#x4E00;&#x4E2A;&#x6362;&#x884C;&#x7B26;&#x800C;&#x4E14;&#x662F;&#x540C;&#x65F6;&#x5237;&#x65B0;&#x8F93;&#x51FA;&#xFF0C;&#x76F8;&#x5F53;&#x4E8E;fflush()&#x51FD;&#x6570;&#xFF09;&#x3002;</p>
<p>&#x7A0B;&#x5E8F;&#x6211;&#x90E8;&#x7F72;&#x8D77;&#x6765;&#x4E86;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;linux&#x7684;nc&#x547D;&#x4EE4;&#x6216;&#x81EA;&#x5DF1;&#x5199;&#x7A0B;&#x5E8F;&#x8FDE;&#x63A5;&#x670D;&#x52A1;&#x5668;&#x6765;&#x67E5;&#x770B;&#x7A0B;&#x5E8F;&#x6548;&#x679C;&#xFF0C;&#x5F53;&#x7136;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;telnet&#x547D;&#x4EE4;&#xFF0C;&#x65B9;&#x6CD5;&#xFF1A;</p>
<p>Linux:</p>
<pre class="language-"><code>nc 120.55.94.78 12345
</code></pre><p>&#x6216;</p>
<pre class="language-"><code>telnet 120.55.94.78 12345
</code></pre><p>&#x7136;&#x540E;&#x5C31;&#x53EF;&#x4EE5;&#x7ED9;&#x670D;&#x52A1;&#x5668;&#x81EA;&#x7531;&#x53D1;&#x9001;&#x6570;&#x636E;&#x4E86;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x4F1A;&#x7ED9;&#x4F60;&#x53D1;&#x9001;&#x7684;&#x4FE1;&#x606F;&#x52A0;&#x4E0A;&#x65F6;&#x95F4;&#x6233;&#x8FD4;&#x56DE;&#x7ED9;&#x4F60;&#x3002;&#x6548;&#x679C;&#x5982;&#x56FE;&#xFF1A;</p>
<p><img src="../imgs/reactor2.jpg" alt=""></p>
<p>&#x53E6;&#x5916;&#x6211;&#x5C06;&#x8FD9;&#x4E2A;&#x4EE3;&#x7801;&#x6539;&#x5199;&#x4E86;&#x6210;&#x7EAF;C++11&#x7248;&#x672C;&#xFF0C;&#x4F7F;&#x7528;CMake&#x7F16;&#x8BD1;&#xFF0C;&#x4E3A;&#x4E86;&#x652F;&#x6301;&#x7F16;&#x8BD1;&#x5FC5;&#x987B;&#x52A0;&#x4E0A;&#x8FD9;-std=c++11&#xFF1A;</p>
<p>CMakeLists.txt&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre class="language-"><code>cmake_minimum_required(VERSION 2.8)

PROJECT(myreactorserver)

AUX_SOURCE_DIRECTORY(./ SRC_LIST)
SET(EXECUTABLE_OUTPUT_PATH ./)

ADD_DEFINITIONS(-g -W -Wall -Wno-deprecated -DLINUX -D_REENTRANT -D_FILE_OFFSET_BITS=64 -DAC_HAS_INFO -DAC_HAS_WARNING -DAC_HAS_ERROR -DAC_HAS_CRITICAL -DTIXML_USE_STL -DHAVE_CXX_STDHEADERS ${CMAKE_CXX_FLAGS} -std=c++11)

INCLUDE_DIRECTORIES(
./
)
LINK_DIRECTORIES(
./
)

set(
main.cpp
myreator.cpp
)

ADD_EXECUTABLE(myreactorserver ${SRC_LIST})

TARGET_LINK_LIBRARIES(myreactorserver pthread)
</code></pre><p>myreactor.h&#x6587;&#x4EF6;&#x5185;&#x5BB9;&#xFF1A;</p>
<pre class="language-"><code>/**
 *@desc: myreactor&#x5934;&#x6587;&#x4EF6;, myreactor.h
 *@author: zhangyl
 *@date: 2016.12.03
 */
#ifndef __MYREACTOR_H__
#define __MYREACTOR_H__

#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>list</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>memory</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>thread</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>mutex</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>condition_variable</span><span class="token punctuation">&gt;</span></span>

#define WORKER_THREAD_NUM   5

class CMyReactor
{
public:
    CMyReactor();
    ~CMyReactor();

    bool init(const char* ip, short nport);
    bool uninit();

    bool close_client(int clientfd);

    static void* main_loop(void* p);

private:
    //no copyable
    CMyReactor(const CMyReactor&amp; rhs);
    CMyReactor&amp; operator = (const CMyReactor&amp; rhs);

    bool create_server_listener(const char* ip, short port);

    static void accept_thread_proc(CMyReactor* pReatcor);
    static void worker_thread_proc(CMyReactor* pReatcor);

private:
    //C11&#x8BED;&#x6CD5;&#x53EF;&#x4EE5;&#x5728;&#x8FD9;&#x91CC;&#x521D;&#x59CB;&#x5316;
    int                             m_listenfd = 0;
    int                             m_epollfd  = 0;
    bool                         m_bStop    = false;

    std::shared_ptr<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">std:</span>:thread</span><span class="token punctuation">&gt;</span></span> m_acceptthread;
    std::shared_ptr<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">std:</span>:thread</span><span class="token punctuation">&gt;</span></span> m_workerthreads[WORKER_THREAD_NUM];

    std::condition_variable         m_acceptcond;
    std::mutex                     m_acceptmutex;

    std::condition_variable         m_workercond ;
    std::mutex                     m_workermutex;

    std::list<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>int</span><span class="token punctuation">&gt;</span></span>                 m_listClients;
};

#endif //!__MYREACTOR_H__
</code></pre><p>myreactor.cpp&#x6587;&#x4EF6;&#x5185;&#x5BB9;&#xFF1A;</p>
<pre class="language-"><code>/** 
 *@desc: myreactor&#x5B9E;&#x73B0;&#x6587;&#x4EF6;, myreactor.cpp
 *@author: zhangyl
 *@date: 2016.12.03
 */
#include &quot;myreactor.h&quot;
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>iostream</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>string.h</span><span class="token punctuation">&gt;</span></span>
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;arpa/inet.h&gt;  //for htonl() and htons()
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>fcntl.h</span><span class="token punctuation">&gt;</span></span>
#include &lt;sys/epoll.h&gt;
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>list</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>errno.h</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>time.h</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>sstream</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>iomanip</span><span class="token punctuation">&gt;</span></span> //for std::setw()/setfill()
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>unistd.h</span><span class="token punctuation">&gt;</span></span>

#define min(a, b) ((a &lt;= b) ? (a) : (b))

CMyReactor::CMyReactor()
{
    //m_listenfd = 0;
    //m_epollfd = 0;
    //m_bStop = false;
}

CMyReactor::~CMyReactor()
{

}

bool CMyReactor::init(const char* ip, short nport)
{
    if (!create_server_listener(ip, nport))
    {
        std::cout &lt;&lt; &quot;Unable to bind: &quot; &lt;&lt; ip &lt;&lt; &quot;:&quot; &lt;&lt; nport &lt;&lt; &quot;.&quot; &lt;&lt; std::endl;
        return false;
    }


    std::cout &lt;&lt; &quot;main thread id = &quot; &lt;&lt; std::this_thread::get_id() &lt;&lt; std::endl;

    //&#x542F;&#x52A8;&#x63A5;&#x6536;&#x65B0;&#x8FDE;&#x63A5;&#x7684;&#x7EBF;&#x7A0B;
    m_acceptthread.reset(new std::thread(CMyReactor::accept_thread_proc, this));

    //&#x542F;&#x52A8;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;
    for (auto&amp; t : m_workerthreads)
    {
        t.reset(new std::thread(CMyReactor::worker_thread_proc, this));
    }


    return true;
}

bool CMyReactor::uninit()
{
    m_bStop = true;
    m_acceptcond.notify_one();
    m_workercond.notify_all();

    m_acceptthread-&gt;join();
    for (auto&amp; t : m_workerthreads)
    {
        t-&gt;join();
    }

    ::epoll_ctl(m_epollfd, EPOLL_CTL_DEL, m_listenfd, NULL);

    //TODO: &#x662F;&#x5426;&#x9700;&#x8981;&#x5148;&#x8C03;&#x7528;shutdown()&#x4E00;&#x4E0B;&#xFF1F;
    ::shutdown(m_listenfd, SHUT_RDWR);
    ::close(m_listenfd);
    ::close(m_epollfd);

    return true;
}

bool CMyReactor::close_client(int clientfd)
{
    if (::epoll_ctl(m_epollfd, EPOLL_CTL_DEL, clientfd, NULL) == -1)
    {
        std::cout &lt;&lt; &quot;close client socket failed as call epoll_ctl failed&quot; &lt;&lt; std::endl;
        //return false;
    }


    ::close(clientfd);

    return true;
}


void* CMyReactor::main_loop(void* p)
{
    std::cout &lt;&lt; &quot;main thread id = &quot; &lt;&lt; std::this_thread::get_id() &lt;&lt; std::endl;

    CMyReactor* pReatcor = static_cast<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>CMyReactor*</span><span class="token punctuation">&gt;</span></span>(p);

    while (!pReatcor-&gt;m_bStop)
    {
        struct epoll_event ev[1024];
        int n = ::epoll_wait(pReatcor-&gt;m_epollfd, ev, 1024, 10);
        if (n == 0)
            continue;
        else if (n &lt; 0)
        {
            std::cout &lt;&lt; &quot;epoll_wait error&quot; &lt;&lt; std::endl;
            continue;
        }

        int m = min(n, 1024);
        for (int i = 0; i &lt; m; ++i)
        {
            //&#x901A;&#x77E5;&#x63A5;&#x6536;&#x8FDE;&#x63A5;&#x7EBF;&#x7A0B;&#x63A5;&#x6536;&#x65B0;&#x8FDE;&#x63A5;
            if (ev[i].data.fd == pReatcor-&gt;m_listenfd)
                pReatcor-&gt;m_acceptcond.notify_one();
            //&#x901A;&#x77E5;&#x666E;&#x901A;&#x5DE5;&#x4F5C;&#x7EBF;&#x7A0B;&#x63A5;&#x6536;&#x6570;&#x636E;
            else
            {
                {
                    std::unique_lock<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">std:</span>:mutex</span><span class="token punctuation">&gt;</span></span> guard(pReatcor-&gt;m_workermutex);
                    pReatcor-&gt;m_listClients.push_back(ev[i].data.fd);
                }

                pReatcor-&gt;m_workercond.notify_one();
                //std::cout &lt;&lt; &quot;signal&quot; &lt;&lt; std::endl;
            }// end if

        }// end for-loop
    }// end while

    std::cout &lt;&lt; &quot;main loop exit ...&quot; &lt;&lt; std::endl;

    return NULL;
}

void CMyReactor::accept_thread_proc(CMyReactor* pReatcor)
{
    std::cout &lt;&lt; &quot;accept thread, thread id = &quot; &lt;&lt; std::this_thread::get_id() &lt;&lt; std::endl;

    while (true)
    {
        int newfd;
        struct sockaddr_in clientaddr;
        socklen_t addrlen;
        {
            std::unique_lock<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">std:</span>:mutex</span><span class="token punctuation">&gt;</span></span> guard(pReatcor-&gt;m_acceptmutex);
            pReatcor-&gt;m_acceptcond.wait(guard);
            if (pReatcor-&gt;m_bStop)
                break;

            //std::cout &lt;&lt; &quot;run loop in accept_thread_proc&quot; &lt;&lt; std::endl;

            newfd = ::accept(pReatcor-&gt;m_listenfd, (struct sockaddr *)&amp;clientaddr, &amp;addrlen);
        }
        if (newfd == -1)
            continue;

        std::cout &lt;&lt; &quot;new client connected: &quot; &lt;&lt; ::inet_ntoa(clientaddr.sin_addr) &lt;&lt; &quot;:&quot; &lt;&lt; ::ntohs(clientaddr.sin_port) &lt;&lt; std::endl;

        //&#x5C06;&#x65B0;socket&#x8BBE;&#x7F6E;&#x4E3A;non-blocking
        int oldflag = ::fcntl(newfd, F_GETFL, 0);
        int newflag = oldflag | O_NONBLOCK;
        if (::fcntl(newfd, F_SETFL, newflag) == -1)
        {
            std::cout &lt;&lt; &quot;fcntl error, oldflag =&quot; &lt;&lt; oldflag &lt;&lt; &quot;, newflag = &quot; &lt;&lt; newflag &lt;&lt; std::endl;
            continue;
        }

        struct epoll_event e;
        memset(&amp;e, 0, sizeof(e));
        e.events = EPOLLIN | EPOLLRDHUP | EPOLLET;
        e.data.fd = newfd;
        if (::epoll_ctl(pReatcor-&gt;m_epollfd, EPOLL_CTL_ADD, newfd, &amp;e) == -1)
        {
            std::cout &lt;&lt; &quot;epoll_ctl error, fd =&quot; &lt;&lt; newfd &lt;&lt; std::endl;
        }
    }

    std::cout &lt;&lt; &quot;accept thread exit ...&quot; &lt;&lt; std::endl;
}

void CMyReactor::worker_thread_proc(CMyReactor* pReatcor)
{
    std::cout &lt;&lt; &quot;new worker thread, thread id = &quot; &lt;&lt; std::this_thread::get_id() &lt;&lt; std::endl;

    while (true)
    {
        int clientfd;
        {
            std::unique_lock<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span><span class="token namespace">std:</span>:mutex</span><span class="token punctuation">&gt;</span></span> guard(pReatcor-&gt;m_workermutex);
            while (pReatcor-&gt;m_listClients.empty())
            {
                if (pReatcor-&gt;m_bStop)
                {
                    std::cout &lt;&lt; &quot;worker thread exit ...&quot; &lt;&lt; std::endl;
                    return;
                }

                pReatcor-&gt;m_workercond.wait(guard);
            }

            clientfd = pReatcor-&gt;m_listClients.front();
            pReatcor-&gt;m_listClients.pop_front();
        }

        //gdb&#x8C03;&#x8BD5;&#x65F6;&#x4E0D;&#x80FD;&#x5B9E;&#x65F6;&#x5237;&#x65B0;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#xFF0C;&#x7528;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x5237;&#x65B0;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#xFF0C;&#x4F7F;&#x4FE1;&#x606F;&#x5728;&#x5C4F;&#x5E55;&#x4E0A;&#x5B9E;&#x65F6;&#x663E;&#x793A;&#x51FA;&#x6765;
        std::cout &lt;&lt; std::endl;

        std::string strclientmsg;
        char buff[256];
        bool bError = false;
        while (true)
        {
            memset(buff, 0, sizeof(buff));
            int nRecv = ::recv(clientfd, buff, 256, 0);
            if (nRecv == -1)
            {
                if (errno == EWOULDBLOCK)
                    break;
                else
                {
                    std::cout &lt;&lt; &quot;recv error, client disconnected, fd = &quot; &lt;&lt; clientfd &lt;&lt; std::endl;
                    pReatcor-&gt;close_client(clientfd);
                    bError = true;
                    break;
                }

            }
            //&#x5BF9;&#x7AEF;&#x5173;&#x95ED;&#x4E86;socket&#xFF0C;&#x8FD9;&#x7AEF;&#x4E5F;&#x5173;&#x95ED;&#x3002;
            else if (nRecv == 0)
            {
                std::cout &lt;&lt; &quot;peer closed, client disconnected, fd = &quot; &lt;&lt; clientfd &lt;&lt; std::endl;
                pReatcor-&gt;close_client(clientfd);
                bError = true;
                break;
            }

            strclientmsg += buff;
        }

        //&#x51FA;&#x9519;&#x4E86;&#xFF0C;&#x5C31;&#x4E0D;&#x8981;&#x518D;&#x7EE7;&#x7EED;&#x5F80;&#x4E0B;&#x6267;&#x884C;&#x4E86;
        if (bError)
            continue;

        std::cout &lt;&lt; &quot;client msg: &quot; &lt;&lt; strclientmsg;

        //&#x5C06;&#x6D88;&#x606F;&#x52A0;&#x4E0A;&#x65F6;&#x95F4;&#x6807;&#x7B7E;&#x540E;&#x53D1;&#x56DE;
        time_t now = time(NULL);
        struct tm* nowstr = localtime(&amp;now);
        std::ostringstream ostimestr;
        ostimestr &lt;&lt; &quot;[&quot; &lt;&lt; nowstr-&gt;tm_year + 1900 &lt;&lt; &quot;-&quot;
            &lt;&lt; std::setw(2) &lt;&lt; std::setfill(&apos;0&apos;) &lt;&lt; nowstr-&gt;tm_mon + 1 &lt;&lt; &quot;-&quot;
            &lt;&lt; std::setw(2) &lt;&lt; std::setfill(&apos;0&apos;) &lt;&lt; nowstr-&gt;tm_mday &lt;&lt; &quot; &quot;
            &lt;&lt; std::setw(2) &lt;&lt; std::setfill(&apos;0&apos;) &lt;&lt; nowstr-&gt;tm_hour &lt;&lt; &quot;:&quot;
            &lt;&lt; std::setw(2) &lt;&lt; std::setfill(&apos;0&apos;) &lt;&lt; nowstr-&gt;tm_min &lt;&lt; &quot;:&quot;
            &lt;&lt; std::setw(2) &lt;&lt; std::setfill(&apos;0&apos;) &lt;&lt; nowstr-&gt;tm_sec &lt;&lt; &quot;]server reply: &quot;;

        strclientmsg.insert(0, ostimestr.str());

        while (true)
        {
            int nSent = ::send(clientfd, strclientmsg.c_str(), strclientmsg.length(), 0);
            if (nSent == -1)
            {
                if (errno == EWOULDBLOCK)
                {
                    std::this_thread::sleep_for(std::chrono::milliseconds(10));
                    continue;
                }
                else
                {
                    std::cout &lt;&lt; &quot;send error, fd = &quot; &lt;&lt; clientfd &lt;&lt; std::endl;
                    pReatcor-&gt;close_client(clientfd);
                    break;
                }

            }

            std::cout &lt;&lt; &quot;send: &quot; &lt;&lt; strclientmsg;
            strclientmsg.erase(0, nSent);

            if (strclientmsg.empty())
                break;
        }
    }
}

bool CMyReactor::create_server_listener(const char* ip, short port)
{
    m_listenfd = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
    if (m_listenfd == -1)
        return false;

    int on = 1;
    ::setsockopt(m_listenfd, SOL_SOCKET, SO_REUSEADDR, (char *)&amp;on, sizeof(on));
    ::setsockopt(m_listenfd, SOL_SOCKET, SO_REUSEPORT, (char *)&amp;on, sizeof(on));

    struct sockaddr_in servaddr;
    memset(&amp;servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr(ip);
    servaddr.sin_port = htons(port);
    if (::bind(m_listenfd, (sockaddr *)&amp;servaddr, sizeof(servaddr)) == -1)
        return false;

    if (::listen(m_listenfd, 50) == -1)
        return false;

    m_epollfd = ::epoll_create(1);
    if (m_epollfd == -1)
        return false;

    struct epoll_event e;
    memset(&amp;e, 0, sizeof(e));
    e.events = EPOLLIN | EPOLLRDHUP;
    e.data.fd = m_listenfd;
    if (::epoll_ctl(m_epollfd, EPOLL_CTL_ADD, m_listenfd, &amp;e) == -1)
        return false;

    return true;
}
</code></pre><p>main.cpp&#x6587;&#x4EF6;&#x5185;&#x5BB9;&#xFF1A;</p>
<pre class="language-"><code>/** 
 *@desc:   &#x7528;reactor&#x6A21;&#x5F0F;&#x7EC3;&#x4E60;&#x670D;&#x52A1;&#x5668;&#x7A0B;&#x5E8F;
 *@author: zhangyl
 *@date:   2016.12.03
 */

#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>iostream</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>signal.h</span><span class="token punctuation">&gt;</span></span>     //for signal()
#include<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>unistd.h</span><span class="token punctuation">&gt;</span></span>
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>stdlib.h</span><span class="token punctuation">&gt;</span></span>        //for exit()
#include &lt;sys/types.h&gt;
#include &lt;sys/stat.h&gt;
#include <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>fcntl.h</span><span class="token punctuation">&gt;</span></span>
#include &quot;myreactor.h&quot;

CMyReactor g_reator;

void prog_exit(int signo)
{
    std::cout &lt;&lt; &quot;program recv signal &quot; &lt;&lt; signo &lt;&lt; &quot; to exit.&quot; &lt;&lt; std::endl; 

    g_reator.uninit();
}

void daemon_run()
{
    int pid;
    signal(SIGCHLD, SIG_IGN);
    //1&#xFF09;&#x5728;&#x7236;&#x8FDB;&#x7A0B;&#x4E2D;&#xFF0C;fork&#x8FD4;&#x56DE;&#x65B0;&#x521B;&#x5EFA;&#x5B50;&#x8FDB;&#x7A0B;&#x7684;&#x8FDB;&#x7A0B;ID&#xFF1B;
    //2&#xFF09;&#x5728;&#x5B50;&#x8FDB;&#x7A0B;&#x4E2D;&#xFF0C;fork&#x8FD4;&#x56DE;0&#xFF1B;
    //3&#xFF09;&#x5982;&#x679C;&#x51FA;&#x73B0;&#x9519;&#x8BEF;&#xFF0C;fork&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x8D1F;&#x503C;&#xFF1B;
    pid = fork();
    if (pid &lt; 0)
    {
        std:: cout &lt;&lt; &quot;fork error&quot; &lt;&lt; std::endl;
        exit(-1);
    }
    //&#x7236;&#x8FDB;&#x7A0B;&#x9000;&#x51FA;&#xFF0C;&#x5B50;&#x8FDB;&#x7A0B;&#x72EC;&#x7ACB;&#x8FD0;&#x884C;
    else if (pid &gt; 0) {
        exit(0);
    }
    //&#x4E4B;&#x524D;parent&#x548C;child&#x8FD0;&#x884C;&#x5728;&#x540C;&#x4E00;&#x4E2A;session&#x91CC;,parent&#x662F;&#x4F1A;&#x8BDD;&#xFF08;session&#xFF09;&#x7684;&#x9886;&#x5934;&#x8FDB;&#x7A0B;,
    //parent&#x8FDB;&#x7A0B;&#x4F5C;&#x4E3A;&#x4F1A;&#x8BDD;&#x7684;&#x9886;&#x5934;&#x8FDB;&#x7A0B;&#xFF0C;&#x5982;&#x679C;exit&#x7ED3;&#x675F;&#x6267;&#x884C;&#x7684;&#x8BDD;&#xFF0C;&#x90A3;&#x4E48;&#x5B50;&#x8FDB;&#x7A0B;&#x4F1A;&#x6210;&#x4E3A;&#x5B64;&#x513F;&#x8FDB;&#x7A0B;&#xFF0C;&#x5E76;&#x88AB;init&#x6536;&#x517B;&#x3002;
    //&#x6267;&#x884C;setsid()&#x4E4B;&#x540E;,child&#x5C06;&#x91CD;&#x65B0;&#x83B7;&#x5F97;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x4F1A;&#x8BDD;(session)id&#x3002;
    //&#x8FD9;&#x65F6;parent&#x9000;&#x51FA;&#x4E4B;&#x540E;,&#x5C06;&#x4E0D;&#x4F1A;&#x5F71;&#x54CD;&#x5230;child&#x4E86;&#x3002;
    setsid();
    int fd;
    fd = open(&quot;/dev/null&quot;, O_RDWR, 0);
    if (fd != -1)
    {
        dup2(fd, STDIN_FILENO);
        dup2(fd, STDOUT_FILENO);
        dup2(fd, STDERR_FILENO);
    }
    if (fd &gt; 2)
        close(fd);
}


int main(int argc, char* argv[])
{  
    //&#x8BBE;&#x7F6E;&#x4FE1;&#x53F7;&#x5904;&#x7406;
    signal(SIGCHLD, SIG_DFL);
    signal(SIGPIPE, SIG_IGN);
    signal(SIGINT, prog_exit);
    signal(SIGKILL, prog_exit);
    signal(SIGTERM, prog_exit);

    short port = 0;
    int ch;
    bool bdaemon = false;
    while ((ch = getopt(argc, argv, &quot;p:d&quot;)) != -1)
    {
        switch (ch)
        {
        case &apos;d&apos;:
            bdaemon = true;
            break;
        case &apos;p&apos;:
            port = atol(optarg);
            break;
        }
    }

    if (bdaemon)
        daemon_run();


    if (port == 0)
        port = 12345;


    if (!g_reator.init(&quot;0.0.0.0&quot;, 12345))
        return -1;

    g_reator.main_loop(&amp;g_reator);

    return 0;
}
</code></pre><p>&#x5B8C;&#x6574;&#x5B9E;&#x4F8B;&#x4EE3;&#x7801;&#x4E0B;&#x8F7D;&#x5730;&#x5740;&#xFF1A;</p>
<p>&#x666E;&#x901A;&#x7248;&#x672C;&#xFF1A;<a href="https://pan.baidu.com/s/1o82Mkno" target="_blank">https://pan.baidu.com/s/1o82Mkno</a></p>
<p>C++11&#x7248;&#x672C;&#xFF1A;<a href="https://pan.baidu.com/s/1dEJdrih" target="_blank">https://pan.baidu.com/s/1dEJdrih</a></p>
<footer class="page-footer"><span class="copyright">&#x5982;&#x9700;&#x4E0B;&#x8F7D;&#x672C;&#x7AD9;&#x5168;&#x90E8;&#x6280;&#x672F;&#x6587;&#x7AE0;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x3010;&#x9AD8;&#x6027;&#x80FD;&#x670D;&#x52A1;&#x5668;&#x5F00;&#x53D1;&#x3011;&#x516C;&#x4F17;&#x53F7;&#x56DE;&#x590D;&#x5173;&#x952E;&#x5B57;&#x201C;&#x6587;&#x7AE0;&#x4E0B;&#x8F7D;&#x201D;&#x5373;&#x53EF;&#x3002;</span><span class="footer-modification">&#x6700;&#x8FD1;&#x66F4;&#x65B0;&#x65F6;&#x95F4;&#xFF1A;
2020-09-20 22:46:31
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
						
					</section>
					
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

				</div>
			</div>
			
		</div>

		
		
		<a href="主线程与工作线程的分工.html" class="navigation navigation-prev " aria-label="Previous page: 主线程与工作线程的分工">
			<i class="fa fa-angle-left"></i>
		</a>
		
		
		<a href="实例：一个服务器程序的架构介绍.html" class="navigation navigation-next " aria-label="Next page: 实例：一个服务器程序的架构介绍">
			<i class="fa fa-angle-right"></i>
		</a>
		
		
		
	</div>
	<script>
		var gitbook = gitbook || [];
		gitbook.push(function() {
			gitbook.page.hasChanged({"page":{"title":"Reactor模式","level":"4.1.2","depth":2,"next":{"title":"实例：一个服务器程序的架构介绍","level":"4.1.3","depth":2,"path":"articles/高性能服务器框架设计/实例：一个服务器程序的架构介绍.md","ref":"articles/高性能服务器框架设计/实例：一个服务器程序的架构介绍.md","articles":[]},"previous":{"title":"主线程与工作线程的分工","level":"4.1.1","depth":2,"path":"articles/高性能服务器框架设计/主线程与工作线程的分工.md","ref":"articles/高性能服务器框架设计/主线程与工作线程的分工.md","articles":[]},"dir":"ltr"},"config":{"plugins":["chapter-fold","-lunr","-search","search-pro","splitter","popup","back-to-top-button","hide-element","prism","-highlight","tbfed-pagefooter","code","anchor-navigation-ex","theme-fexa","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"如需下载本站全部技术文章，可以在【高性能服务器开发】公众号回复关键字“文章下载”即可。","modify_label":"最近更新时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"prism":{},"livereload":{},"splitter":{},"search-pro":{},"popup":{},"theme-fexa":{"search-placeholder":"输入关键字搜索","logo":"./logo.png","favicon":"./favicon.ico"},"code":{"copyButtons":true},"hide-element":{"elements":[".gitbook-link"]},"fontsettings":{"theme":"white","family":"sans","size":2},"anchor-navigation-ex":{"showLevel":false,"associatedWithSummary":true,"mode":"float","showGoTop":false,"printLog":false,"multipleH1":true,"float":{"floatIcon":"fa fa-navicon","showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""},"pageTop":{"showLevelIcon":false,"level1Icon":"","level2Icon":"","level3Icon":""}},"back-to-top-button":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"easy_coder","pdf":{"pageBreaksBefore":"/","headerTemplate":null,"paperSize":"a4","margin":{"right":62,"left":62,"top":36,"bottom":36},"fontSize":12,"fontFamily":"Arial","footerTemplate":null,"chapterMark":"pagebreak","pageNumbers":false},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"isbn":"","variables":{},"title":"高性能服务器开发 技术专栏","links":{},"gitbook":"*","description":"","theme-default":{"showLevel":true},"extension":null},"file":{"path":"articles/高性能服务器框架设计/Reactor模式.md","mtime":"2020-09-20T14:46:31.642Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-10-09T15:31:57.905Z"},"basePath":"../..","book":{"language":""}});
		});
	</script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-theme-fexa/fexa.js"></script>
        
    

    </body>
</html>

